package com.lottery.material.controller.admin.circle;

import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.enumeration.common.CommonEnum;
import com.lottery.material.pojo.circle.CircleInfo;
import com.lottery.material.pojo.circle.CircleNoticeInfo;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojo.page.PageData;
import com.lottery.material.pojocustom.circle.CircleNoticeInfoCustom;
import com.lottery.material.service.admin.circle.CircleInfoService;
import com.lottery.material.service.admin.circle.CircleNoticeInfoService;
import com.lottery.material.util.AssertUtil;
import com.lottery.material.util.Jurisdiction;
import com.lottery.material.util.PdUtil;
import com.lottery.material.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Description：圈子公告信息Controller
 * Created by 朱正磊 on 2019-04-13 20:16
 */
@Controller("adminCircleNoticeInfoController")
@RequestMapping("/admin/circleNoticeInfo")
public class CircleNoticeInfoController extends BaseController {

    //菜单地址，用于权限控制
    private String menuUrl = "admin/circleNoticeInfo/list";

    private final CircleNoticeInfoService circleNoticeInfoService;

    private final CircleInfoService circleInfoService;

    @Autowired
    public CircleNoticeInfoController(CircleNoticeInfoService circleNoticeInfoService,
                                      CircleInfoService circleInfoService) {
        this.circleNoticeInfoService = circleNoticeInfoService;
        this.circleInfoService = circleInfoService;
    }

    /**
     * description：分页列表
     * user 朱正磊
     * time 2019-04-17 15:06
     * @param page 分页对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/list")
    public ModelAndView listPage(Page page, Map<String, Object> map) {
        //分页对象设置页面请求参数
        PageData pd = getPageData();
        PdUtil.rePut(pd);
        pd.put("isDelete", CommonEnum.IS_DELETE_FALSE.getValue());//查询未逻辑删除的数据
        page.setPd(pd);

        //查询分页列表
        List<CircleNoticeInfoCustom> list = circleNoticeInfoService.findCustomListPage(page);
        map.put("list", list);
        map.put("pd", pd);
        map.put("QX", Jurisdiction.getHC());

        return new ModelAndView("circle/circleNoticeInfo/listPage", map);
    }

    /**
     * description：详情页面
     * user 朱正磊
     * time 2019-04-17 16:20
     * @param id 主键
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/detailPage")
    public ModelAndView detailPage(Integer id, Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")) {//判断是否有查看权限
            return null;
        }

        //根据主键查询扩展类
        CircleNoticeInfoCustom circleNoticeInfo = circleNoticeInfoService.findCustomById(id);
        map.put("circleNoticeInfo", circleNoticeInfo);

        return new ModelAndView("circle/circleNoticeInfo/detailPage", map);
    }

    /**
     * description：添加页面
     * user 朱正磊
     * time 2019-04-17 16:26
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/addPage")
    public ModelAndView addPage(Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {//判断是否有添加权限
            return null;
        }

        //上架的圈子列表
        List<CircleInfo> circleInfoList = circleInfoService.findListByState((Integer) CommonEnum.STATE_ON.getValue());
        map.put("circleInfoList", circleInfoList);

        //添加请求地址
        map.put("actionUrl", "admin/circleNoticeInfo/add");

        return new ModelAndView("circle/circleNoticeInfo/editPage", map);
    }

    /**
     * description：添加
     * user 朱正磊
     * time 2019-04-17 16:39
     * @param circleNoticeInfo 对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/add")
    public ModelAndView add(CircleNoticeInfo circleNoticeInfo, Map<String, Object> map) {
        Integer flag = circleNoticeInfoService.add(circleNoticeInfo);
        if (Objects.equals(flag, 0)) { //添加失败
            throw new RuntimeException(ResultEnum.STATUS_PERSISTENT_OPERATE_SQL_FAIL.getMessage());
        } else {
            map.put("msg","success");
            return new ModelAndView("save_result", map);
        }
    }

    /**
     * description：修改页面
     * user 朱正磊
     * time 2019-04-17 16:48
     * @param id 主键
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/editPage")
    public ModelAndView editPage(Integer id, Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {//判断是否有修改权限
            return null;
        }

        //根据主键查询扩展类
        CircleNoticeInfoCustom circleNoticeInfo = circleNoticeInfoService.findCustomById(id);
        map.put("circleNoticeInfo", circleNoticeInfo);

        //上架的圈子列表
        List<CircleInfo> circleInfoList = circleInfoService.findListByState((Integer) CommonEnum.STATE_ON.getValue());
        map.put("circleInfoList", circleInfoList);

        //修改请求地址
        map.put("actionUrl", "admin/circleNoticeInfo/edit");

        return new ModelAndView("circle/circleNoticeInfo/editPage", map);
    }

    /**
     * description：修改
     * user 朱正磊
     * time 2019-04-17 16:58
     * @param circleNoticeInfo 对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/edit")
    public ModelAndView edit(CircleNoticeInfo circleNoticeInfo, Map<String, Object> map) {
        Integer flag = circleNoticeInfoService.updateById(circleNoticeInfo);
        if (Objects.equals(flag, 0)) { //修改失败
            throw new RuntimeException(ResultEnum.STATUS_PERSISTENT_OPERATE_SQL_FAIL.getMessage());
        } else {
            map.put("msg","success");
            return new ModelAndView("save_result", map);
        }
    }

    /**
     * description：根据主键删除一条数据（逻辑删除）
     * user 朱正磊
     * time 2019-04-30 11:26
     * @param id 主键
     * @return 返回响应对象
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseResult delete(Integer id) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "del")) {//判断是否有删除权限
            return null;
        }
        Integer flag = circleNoticeInfoService.deleteLogicById(id);
        if (Objects.equals(0, flag)) {
            return ResponseResult.buildResult(400, "删除失败！");
        } else {
            return ResponseResult.buildResult(200, "删除成功！");
        }
    }

    /**
     * description：验证是否允许新增和修改数据，
     * 如果相应的圈子下面已经新增过了3条未逻辑删除的圈子公告数据，就不允许再次新增；
     * 在修改的时候，如果修改了圈子，而相应的圈子下面已经有了3条未逻辑删除的圈子公告数据，就提示不允许修改。
     * user 朱正磊
     * time 2019-05-28 14:38
     * @param request 请求对象
     * @return 返回响应对象
     */
    @RequestMapping("/validateAdd")
    @ResponseBody
    public ResponseResult validateAdd(HttpServletRequest request) {
        String id = request.getParameter("id");
        if (AssertUtil.isNotEmpty(id)) {//修改
            Integer paramId = Integer.valueOf(id);//请求id
            CircleNoticeInfo config = circleNoticeInfoService.findById(paramId);
            //数据库保存的圈子ID
            Integer dataCircleId = config.getCircleId();
            //页面请求的圈子ID
            Integer paramCircleId = Integer.valueOf(request.getParameter("circleId"));
            if (Objects.equals(dataCircleId, paramCircleId)) {//没有修改圈子
                return ResponseResult.buildResult(200);
            }
            List<CircleNoticeInfo> circleNoticeInfoList = circleNoticeInfoService
                    .findByCircleIdAndIsDelete(paramCircleId, (Integer) CommonEnum.IS_DELETE_FALSE.getValue());
            Integer size = circleNoticeInfoList.size();
            if (Objects.equals(size, 3)) {//想要修改成的圈子已经有了3条未逻辑删除的数据
                return ResponseResult.buildResult(400, "该圈子下已经有了3条公告数据，不允许修改成该圈子！");
            }
        }
        Integer circleId = Integer.valueOf(request.getParameter("circleId"));//圈子ID
        List<CircleNoticeInfo> circleNoticeInfoList = circleNoticeInfoService
                .findByCircleIdAndIsDelete(circleId, (Integer) CommonEnum.IS_DELETE_FALSE.getValue());
        Integer size = circleNoticeInfoList.size();
        if (Objects.equals(size, 3)) {//之前已经新增了3条未逻辑删除的数据
            return ResponseResult.buildResult(400, "该圈子之前已经新增了3条公告数据，不再允许新增新的公告！");
        }
        return ResponseResult.buildResult(200);
    }

}
